package com.lsy.android.motu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

public class DetailActivity extends Activity {

	private String mUrl;
	private WebView mWebContent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		initView();
	}

	private void initView() {
		mWebContent = (WebView) findViewById(R.id.web_content);
		mWebContent.getSettings().setJavaScriptEnabled(true);
		mWebContent.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Log.e("test", url);
				view.loadUrl(url);
				mUrl = url;
				return true;
			}
		});
		String url = getIntent().getStringExtra("url");
		if (!TextUtils.isEmpty(url)) {
			mWebContent.loadUrl(url);
			mUrl = url;
		} else {
			finish();
		}
		LinearLayout back = (LinearLayout) findViewById(R.id.detail_back);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DetailActivity.this.finish();
			}
		});
		LinearLayout share = (LinearLayout) findViewById(R.id.detail_share);
		share.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.setType("text/plain");
				intent.putExtra(Intent.EXTRA_TEXT, mUrl);
				startActivity(intent);
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			if(mWebContent.canGoBack()){
				mWebContent.goBack();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

}
